<?php
namespace app\admin\controller\csmwj;

use think\Db;
use Exception;
use think\exception\PDOException;
use app\common\controller\Backend;
use think\exception\ValidateException;
use addons\csmwj\library\xcore\xcore\utils\XcRequestUtils;
use addons\csmwj\library\xcore\xcore\utils\XcAdminTabUtils;

/**
 * 工单
 *
 * @icon fa fa-circle-o
 */
class Papercfg extends Backend
{

    /**
     * Papercfg模型对象
     *
     * @var \app\admin\model\csmwj\Papercfg
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\csmwj\Papercfg();
        $this->view->assign("localesList", $this->model->getLocalesList());
        $this->view->assign("cfghasdatelimitList", $this->model->getCfghasdatelimitList());
        $this->view->assign("cfghastimelimitList", $this->model->getCfghastimelimitList());
        $this->view->assign("statusList", $this->model->getStatusList());

        // csmwj:设置页面tabs
        $id = XcRequestUtils::param("ids");
        XcAdminTabUtils::append_control_initialize('paper', $id, __CLASS__, $this->view);
    }

    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            if (!in_array($row[$this->dataLimitField], $adminIds)) {
                $this->error(__('You have no permission'));
            }
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $params = $this->preExcludeFields($params);


                //如果设置了工单有有效期,则有效期时间不能为空,且截止日期需要大于开始时间
                if($params['cfghasdatelimit']=='Y'){
                    if(empty($params['cfgbegintime'])){
                        $this->error('开始时间不能为空');
                    }
                    if(empty($params['cfgendtime'])){
                        $this->error('截止时间不能为空');
                    }
                    if(strtotime($params['cfgbegintime'])>=strtotime($params['cfgendtime'])){
                        $this->error('开始时间不能晚于截止时间');
                    }
                }

                $result = false;
                Db::startTrans();
                try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
                        $row->validateFailException(true)->validate($validate);
                    }
                    $result = $row->allowField(true)->save($params);
                    Db::commit();
                } catch (ValidateException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (PDOException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if ($result !== false) {
                    $this->success();
                } else {
                    $this->error(__('No rows were updated'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }


    //列表
    public function index()
    {
        return;
    }
    //添加
    public function add()
    {
        return;
    }

    //删除
    public function del($ids = null)
    {
        return;
    }
    //回收站列表
    public function recyclebin()
    {
        return;
    }
    //回收站(真实删除或清空)
    public function destroy($ids = null)
    {
        return;
    }
    //回收站还原
    public function restore($ids = null)
    {
        return;
    }
    //批量操作(修改状态)
    public function multi($ids = null)
    {
        return;
    }
}
